<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\UserAddRequest;
use App\Http\Requests\Admin\UserEditRequest;
use App\Http\Requests\Home\RegRequest;
use App\Model\Earn;
use App\Model\Tixian;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    protected $fields = [
        'username' => '',
        'name' => '',
        'mobile' => '',
        'code' => '',
        'card_pwd' => 123456,
        'password' => 123456,
    ];

    public function index(Request $request)
    {
        $name = trim($request->name,'');
        $search = array(
            'name' => $name,
        );
        $sql = User::query();
        $name && $sql->where('name','like','%'.$name.'%');
        $data = $sql->where('deleted',0)->orderBy('id','desc')->paginate(20);
        foreach ($data as $k => $v){
            $total = Earn::where('uid',$v->id)->sum('money');
            $tixian = Tixian::where('uid',$v->id)->sum('money');
            $balance = $total - $tixian;
            $data[$k]->balance = sprintf('%.2f',$balance/100);
        }
        return view('admin.user.index',['data'=>$data,'search'=>$search]);

    }

    public function add()
    {
        $data = [];
        foreach ($this->fields as $field => $default) {
            $data[$field] = old($field, $default);
        }
        $data['username'] = $this->get_username();

        return view('admin.user.add',['data'=>$data]);
    }

    private function get_username(){
        $username = 'JD'.create_guid();
        $info = User::where('username',$username)->first();
        if(!empty($info)){
            $this->get_username();
        }else{
            return $username;
        }
    }

    public function add_post(UserAddRequest $request){
        $info = new User();
        foreach (array_keys($this->fields) as $field) {
            $info->$field = !empty($request->input($field)) ?$request->input($field) : '';
        }
        $info->remember_token = '';
        $info->status = 0;
        $info->deleted = 0;
        $info->save();
        return redirect('/admin/user/index')->withSuccess('添加成功！');
    }

    public function edit($id)
    {
        $info = User::find($id);
        if(!$info){
            return redirect('/admin/user/index')->withErrors('找不到该用户!');
        }
        foreach (array_keys($this->fields) as $field) {
            $data[$field] = old($field, $info->$field);
        }
        $data['id'] = (int)$id;
        return view('admin.user.edit', ['data'=>$data]);
    }

    public function edit_post(UserEditRequest $request)
    {
        $id = $request->input('id');
        $info = User::find((int)$id);
        foreach (array_keys($this->fields) as $field) {
            $info->$field = !empty($request->input($field)) ?$request->input($field) : '';
        }
        $info->save();
        return redirect('/admin/user/index')->withSuccess('编辑成功！');
    }

    public function delete($id)
    {
        $tag = User::find((int)$id);
        if ($tag) {
            $tag->deleted = -1;
            $tag->save();
        } else {
            return redirect()->back()
                ->withErrors("删除失败");
        }

        return redirect()->back()
            ->withSuccess("删除成功");
    }

}